package com.syyo.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.syyo.gulimall.order.domain.entity.OrderEntity;
import com.syyo.gulimall.order.domain.entity.OrderReturnReasonEntity;
import com.syyo.gulimall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Auther: wangzhong
 * @Date: 2020/7/30 10:54
 * @Description:
 */
@Slf4j
@Component
//@RabbitListener(queues = "hello-queue")
public class OrderListener {

    @Autowired
    OrderService orderService;

    @RabbitListener(queues = "order.release.order.queue")
    public void closeOrderHandler(OrderEntity entity,
                                  Message msg,
                                  Channel channel) throws IOException {

        System.out.println("===============收到过期的订单消息，准备关闭订单:"+entity.getOrderSn()+"===============");
        try {
            // 当消息是第二次及以后派发的就为true
//            Boolean redelivered = msg.getMessageProperties().getRedelivered();
            orderService.closeOrder(entity);
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 发送请求失败,重新回队
            channel.basicReject(msg.getMessageProperties().getDeliveryTag(), true);
            System.out.println("================2==============");
        }


    }


    /**
     * 发送华信短信的消息监听器
     * queues ：监听的队列
     * <p>
     * 接收的消息类型：org.springframework.amqp.core.Message;
     * 参数类型
     * 1.Message  原生消息详情，消息头 、消息体
     * 2.T<发送消息的类型>
     * 3.Channel :传输数据的通道               com.rabbitmq.client.Channel
     */
//    @RabbitListener(queues = "hello-queue")
//    @RabbitHandler
    public void listenTest(Message msg,
                           OrderReturnReasonEntity entity,
                           Channel channel) {
        byte[] body = msg.getBody();
        MessageProperties properties = msg.getMessageProperties();


        System.out.println("接收到原生消息=" + msg);
        System.out.println("接收到自定义消息=" + entity);
        long deliveryTag = properties.getDeliveryTag();

        try {
            // 确认消息，收货
            channel.basicAck(deliveryTag, false);

            // 拒接，退货
            channel.basicNack(deliveryTag, false, false);
        } catch (IOException e) {
            // 网络中断
            e.printStackTrace();
        }
    }


    //    @RabbitHandler
    public void handlerTest(Message msg,
                            OrderEntity entity,
                            Channel channel) {
        byte[] body = msg.getBody();
        MessageProperties properties = msg.getMessageProperties();

        System.out.println("接收到原生消息=" + msg);
        System.out.println("接收到自定义消息=" + entity);
    }

    //    @RabbitListener(queues = "order.release.queue")
    public void handlerTestOrder(OrderEntity entity,
                                 Message msg,
                                 Channel channel) {
        byte[] body = msg.getBody();
        MessageProperties properties = msg.getMessageProperties();
        System.out.println("===============收到消息，准备关闭订单===============");
        System.out.println("接收到原生消息=" + msg);
        System.out.println("接收到自定义消息=" + entity);
        long deliveryTag = properties.getDeliveryTag();
        try {
            // 确认消息，收货
            channel.basicAck(deliveryTag, false);

            // 拒接，退货
            channel.basicNack(deliveryTag, false, false);
        } catch (IOException e) {
            // 网络中断
            e.printStackTrace();
        }
    }
}
